# ========================================
# AES 密钥长度验证工具
# ========================================
# 
# 用法: .\verify-key.ps1 '你的密钥'
# 示例: .\verify-key.ps1 'campus-aes-key!'
# ========================================

param([string]$TestKey)

if (-not $TestKey) {
    Write-Host ""
    Write-Host "========================================" -ForegroundColor Cyan
    Write-Host "AES 密钥长度验证工具" -ForegroundColor Cyan
    Write-Host "========================================" -ForegroundColor Cyan
    Write-Host ""
    Write-Host "用法:" -ForegroundColor Yellow
    Write-Host "  .\verify-key.ps1 '你的密钥'" -ForegroundColor White
    Write-Host ""
    Write-Host "示例:" -ForegroundColor Yellow
    Write-Host "  .\verify-key.ps1 'campus-aes-key!'" -ForegroundColor White
    Write-Host "  .\verify-key.ps1 'lost-found-2024!'" -ForegroundColor White
    Write-Host ""
    Write-Host "或者直接输入密钥进行验证：" -ForegroundColor Yellow
    $TestKey = Read-Host "请输入要验证的密钥"
    
    if (-not $TestKey) {
        Write-Host "✗ 未输入密钥，退出" -ForegroundColor Red
        exit
    }
}

# 计算长度
$charLength = $TestKey.Length
$byteLength = [System.Text.Encoding]::UTF8.GetByteCount($TestKey)

# 显示结果
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "密钥验证结果" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "密钥内容: " -NoNewline
Write-Host "'$TestKey'" -ForegroundColor White
Write-Host ""
Write-Host "字符长度: " -NoNewline
if ($charLength -eq 16) {
    Write-Host "$charLength 字符 ✓" -ForegroundColor Green
} else {
    Write-Host "$charLength 字符 ✗" -ForegroundColor Red
}

Write-Host "字节长度: " -NoNewline
if ($byteLength -eq 16) {
    Write-Host "$byteLength 字节 ✓" -ForegroundColor Green
} else {
    Write-Host "$byteLength 字节 ✗" -ForegroundColor Red
}
Write-Host ""

# 逐字符显示
Write-Host "字符详情:" -ForegroundColor Yellow
$chars = $TestKey.ToCharArray()
for ($i = 0; $i -lt $chars.Length; $i++) {
    $char = $chars[$i]
    $charBytes = [System.Text.Encoding]::UTF8.GetByteCount([string]$char)
    Write-Host "  位置 $($i+1): '$char' (字节: $charBytes)" -ForegroundColor Gray
}
Write-Host ""

# 最终判断
Write-Host "========================================" -ForegroundColor Cyan
if ($charLength -eq 16 -and $byteLength -eq 16) {
    Write-Host "✓ 密钥长度正确！可以使用" -ForegroundColor Green
    Write-Host "========================================" -ForegroundColor Cyan
    Write-Host ""
    Write-Host "IDEA 环境变量配置:" -ForegroundColor Yellow
    Write-Host "AES_ENCRYPTION_KEY=$TestKey" -ForegroundColor White
    Write-Host ""
    Write-Host "PowerShell 配置:" -ForegroundColor Yellow
    Write-Host "`$env:AES_ENCRYPTION_KEY=`"$TestKey`"" -ForegroundColor White
    Write-Host ""
} else {
    Write-Host "✗ 密钥长度错误！" -ForegroundColor Red
    Write-Host "========================================" -ForegroundColor Cyan
    Write-Host ""
    
    if ($charLength -lt 16) {
        $needed = 16 - $charLength
        Write-Host "当前长度不足，需要增加 $needed 个字符" -ForegroundColor Yellow
        
        # 提供修正建议
        Write-Host ""
        Write-Host "修正建议:" -ForegroundColor Cyan
        $padding = "!" * $needed
        Write-Host "  $TestKey$padding" -ForegroundColor White
        Write-Host "  (在末尾添加 $needed 个感叹号)" -ForegroundColor Gray
        
    } elseif ($charLength -gt 16) {
        $extra = $charLength - 16
        Write-Host "当前长度过长，需要删除 $extra 个字符" -ForegroundColor Yellow
        
        # 提供修正建议
        Write-Host ""
        Write-Host "修正建议:" -ForegroundColor Cyan
        $fixed = $TestKey.Substring(0, 16)
        Write-Host "  $fixed" -ForegroundColor White
        Write-Host "  (保留前 16 个字符)" -ForegroundColor Gray
    }
    Write-Host ""
}

Write-Host ""
Write-Host "推荐的 16 字节密钥示例:" -ForegroundColor Yellow
Write-Host "  campus-aes-key!" -ForegroundColor White
Write-Host "  lost-found-2024!" -ForegroundColor White
Write-Host "  1234567890123456" -ForegroundColor White
Write-Host "  My16ByteKey@2024" -ForegroundColor White
Write-Host ""

